/**
 * @angular 管理员
 * @description
 * @auth yang li
 */

menuManage = function ($scope, $ducTools, $compile, http, $dataTableSet){
    'use strict';
    var menuManageTable = $('#menuManageTable').DataTable({
        sAjaxSource: 'system/menu/query',
		fnServerData:  $ducTools.retrieveData,
        fnServerParams: function (aoData) {
            aoData.push(
                { "name": "menuName", "value": $scope.menuNameS},
				{ "name": "menuPath", "value": $scope.menuPathS},
				{ "name": "parentId", "value": $scope.parentIdS}
            )
        },
        columns: [
            {"data": "id"},
            {"data": "menuName"},
            {"data": "menuPath"},
            {"data": "parentName"},
            {"data": "orderNum"},
		    {"data": null},
        ],
     	columnDefs: [
            {
                targets: 5,
                render: function (c) {
					return "<button class='btn btn-success btn-xs' ng-click=update('"+c.id+"')> 修改 </button> &nbsp;" + 
							"<button class='btn btn-info btn-xs' ng-click=deleteList('"+c.id+"')> 删除 </button> &nbsp;";
                }
            }
        ],
        createdRow: function( row, data, dataIndex ) {
            $compile(row)($scope);
        }
    });

    $dataTableSet.dataTableRowShow('menuManageTable');

    $scope.accountRecordDraw = function() {
        menuManageTable.draw();
    };
	
	//获得父菜单
	http.post({"parentId":"0"}, '/system/menu/query').then(function (res){
    	$scope.parentList = res;
    });
	
	//删除菜单
	$scope.deleteList = function (id){
		
		swal({
			title: "确定要删除吗？",
			showCancelButton: true,
			closeOnConfirm: false,
			confirmButtonText: "确定",
			confirmButtonColor: "#046dae",
			cancelButtonText: "取消",
			animation: "slide-from-top"
		},function () {
			http.post({"id":id}, 'system/menu/delete').then(function (res){
				swal("删除成功！");
				menuManageTable.draw();	
			});
		});
		
		
    };
	
	//添加修改
	$scope.addUser = function (id){
		$scope.adminId = "";
		$(id).modal('show').find("form")[0].reset();
    };
	$scope.update = function(id) {
        $("#adminListModal1").modal('show');
		$scope.adminId = id
		http.post({"id":id}, 'system/menu/get').then(function (res){
			if(res.parentId == undefined){
				$scope.parentId = "";		
			}else{
				$scope.parentId = res.parentId;	
			}
			$scope.menuName = res.menuName;
			$scope.menuPath = res.menuPath;
			$scope.orderNum = res.orderNum;	
		});
    }
	$("#addAdminForm1").validate({
		rules:{
            menuName: 'required'
        },
        messages:{
            menuName:{
                required:"请输入菜单名称！"
            }
        },
        focusInvalid: false,
        onkeyup: false,
        submitHandler: function(){
			var _url;
			var data;
			
			if($scope.adminId == ""){
				_url = 'system/menu/add'
				data = {
					"parentId":$scope.parentId,
					"menuName":$scope.menuName,
					"menuPath":$scope.menuPath,
					"orderNum":$scope.orderNum,
				};
			}else{
				_url = 'system/menu/mod'
				data = {
					"id":$scope.adminId,
					"parentId":$scope.parentId,
					"menuName":$scope.menuName,
					"menuPath":$scope.menuPath,
					"orderNum":$scope.orderNum,
				};	
			}	
            http.post(data, _url).then(function (result) {
                $('#adminListModal1').modal('hide');
				swal("保存成功！")
                menuManageTable.draw();
            })
        },
        errorPlacement: function(error, element) {
            var $addAdminFormErrorMsg = $("#addAdminFormErrorMsg");
            if(!$addAdminFormErrorMsg.find('label').is(':visible')){
                $addAdminFormErrorMsg.html(error);
            }
            return false;
        }
    });
	
};
